"""
https://openpyxl.readthedocs.io/en/stable/charts/scatter.html
"""
from openpyxl import Workbook
from openpyxl.chart import ScatterChart, Reference, Series

# 创建工作簿
workbook = Workbook()
worksheet = workbook.active

worksheet.title = 'Scatter Chart'

# 准备数据
rows = [
    ['Size', 'Batch 1', 'Batch 2'],
    [2, 40, 30],
    [3, 40, 25],
    [4, 50, 30],
    [5, 30, 25],
    [6, 25, 35],
    [7, 20, 40],
]

for row in rows:
    worksheet.append(row)

# 绘制散点图
chart = ScatterChart()
chart.title = 'Scatter Chart'
chart.style = 13
chart.x_axis.title = 'Size'
chart.y_axis.title = 'Percentage'

xValues = Reference(worksheet, min_col=1, min_row=2, max_row=7)
for index in range(2, 4):
    yValues = Reference(worksheet, min_col=index, min_row=1, max_row=7)
    series = Series(values=yValues, xvalues=xValues, title_from_data=True)

    chart.series.append(series)

worksheet.add_chart(chart)

# 保存工作簿
workbook.save('ScatterChart.xlsx')
